package com.sgb.kjwl.view.ui.fragment.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.bumptech.glide.Glide;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentTitleItem2Binding;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.kjwl.view.ui.adapter.home.TitleItemAdapter;
import com.swgk.core.base.di.AppComponent;

import java.util.ArrayList;
import java.util.List;

public class TitleItemFragment2 extends LazyFragment2 {

    private FragmentTitleItem2Binding binding;
    private static final int ITEM_COUNT = 7;

    private List<MenuEntity> menuEntities = new ArrayList<>();
    private TitleItemAdapter.OnItemClick<MenuEntity> onItemClick;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_title_item_2, container, false);
        initView();
        return binding.getRoot();
    }

    @Override
    protected void onMultiClick(View v) {
        if (onItemClick == null || menuEntities.size() < 7) {
            return;
        }
        switch (v.getId()) {
            case R.id.ll_container_3:
                onItemClick.onItemClick(menuEntities.get(3), 3);
                break;
            case R.id.ll_container_4:
                onItemClick.onItemClick(menuEntities.get(4), 4);
                break;
            case R.id.ll_container_5:
                onItemClick.onItemClick(menuEntities.get(5), 5);
                break;
            case R.id.ll_container_6:
                onItemClick.onItemClick(menuEntities.get(6), 6);
                break;
            default:
                break;
        }
    }

    @Override
    protected void initView() {
        binding.llContainer3.setOnClickListener(this);
        binding.llContainer4.setOnClickListener(this);
        binding.llContainer5.setOnClickListener(this);
        binding.llContainer6.setOnClickListener(this);
    }

    public void loadData(List<MenuEntity> menuEntities) {

        if (menuEntities != null && menuEntities.size() >= ITEM_COUNT) {
            this.menuEntities = menuEntities;
            Glide.with(context).load(menuEntities.get(3).getAppIcon()).into(binding.imageView3);
            binding.tvName3.setText(menuEntities.get(3).getName());

            Glide.with(context).load(menuEntities.get(4).getAppIcon()).into(binding.imageView4);
            binding.tvName4.setText(menuEntities.get(4).getName());

            Glide.with(context).load(menuEntities.get(5).getAppIcon()).into(binding.imageView5);
            binding.tvName5.setText(menuEntities.get(5).getName());

            Glide.with(context).load(menuEntities.get(6).getAppIcon()).into(binding.imageView6);
            binding.tvName6.setText(menuEntities.get(6).getName());
        }
    }

    public void setOnItemClick(TitleItemAdapter.OnItemClick<MenuEntity> onItemClick) {
        this.onItemClick = onItemClick;
    }

}